define([
	'selectModify',
	'layer',
	'../../Shop/module/STH_main.js',
	'checkAll',
	'iCheck',
	'imgCenter'
], function(selectModify, layer, main) {

	return {

		//商品图片居中函数
		goodsImgCenter: function(thisPanel, thisEdit, key) {
			$(thisPanel).find(".goods-list.style-small .goods-img, .goods-list.style-big .goods-img").each(function() {
				$(this).height($(this).parents("li").width());
			});
			$(thisPanel).find(".goods-list.style-list .goods-img").each(function() {
				$(this).height(80);
			});
			$(thisPanel).find('.goods-img').imgCenter();
		},

		//加入模块
		goodsAddM: function(con) {
			var _this = this,
				key = con.key,
				prop = con.prop,
				order = con.order,
				ItemList = con.PList,

				showprice = prop.showprice,
				titleshow = prop.titleshow,
				size = prop.size,
				moreUrl = prop.moreUrl,
				group_id_1 = prop.group_id_1,
				group_id_2 = prop.group_id_2,

				newPanel = main.getPanel("goods"),
				newEdit = main.getEdit("goods");

			//右侧edit属性
			$(newEdit).attr("key", key);
			$(newEdit).find("input[name='size'][value='" + size + "']").attr("checked", 'checked');
			
			
			if(showprice == 1) {
				$(newEdit).find("input[name='showprice'][value='1']").attr("checked", 'checked');
			} else {
				$(newEdit).find("input[name='showprice'][value='1']").prop("checked", false);
			}

			$(newEdit).css("display", "none");

			//左侧panel属性
			$(newPanel).attr("order", order);
			$(newPanel).attr("key", key);
			$(newPanel).find(".goods-list").attr("class", "goods-list style-" + size);
			//			$(newPanel).find(".goods-list h3 a h4").html(list_name);
			//			$(newPanel).find(".goods-list h3 a").attr("href", moreUrl);

			//插入panel(延时顺序重定位)
			var edLength = $(".hb_box .panel-comp").length;
			if($(".hb_box .panel-comp").length < 1 || order > parseInt($(".hb_box .panel-comp").eq(edLength - 1).attr("order"))) {
				$(".hb_box").append(newPanel);
			} else if(order < parseInt($(".hb_box .panel-comp").eq(0).attr("order"))) {
				$(".hb_box").prepend(newPanel);
			} else {
				for(var r = 0; r < edLength; r++) {
					if(order > parseInt($(".hb_box .panel-comp").eq(r).attr("order")) && order < parseInt($(".hb_box .panel-comp").eq(r + 1).attr("order"))) {
						$(".hb_box .panel-comp").eq(r).after(newPanel);
						break;
					}
				}
			}
			
			//插入edit
			$(".edit-panel").append(newEdit);

			//商品循环再拉取
			var newPanelLi = $(newPanel).find(".moban").html(),
				newEditLi = $(newEdit).find(".moban").html();

			$(newEdit).find(".good-list-show ul").html("");
			$(newPanel).find(".goods-list ul").html("");
			for(var d = 0; d < ItemList.length; d++) {
				var row = ItemList[d];
				var newobj = {
						"Fhimg": row.Fhimg,
						"Fid": row.Fid,
						"Fsummary": row.Fsummary,
						"Flowprice": row.Flowprice,
						"Fbrand": row.Fbrand,
						"Fpersonnum": row.Fpersonnum
					},
					nPLi = main.formatTemplate(newobj, newPanelLi),
					nELi = main.formatTemplate(newobj, newEditLi);

				$(newPanel).find(".goods-list ul").append(nPLi);
				$(newEdit).find(".good-list-show ul").append(nELi);

				$(newPanel).find(".goods-list ul li").eq(d).find('.goods-stock').show();

				//图片居中
				_this.goodsImgCenter(newPanel, newEdit, key);
				g_data[key].ItemList.push({
							"Ftype": 3,
							"Ftypeid": row.Fid
						});
			}
			g_data[key].prop.size=size;
			//调用本模块的操作  
			_this.goodsActive(newPanel, newEdit, key);
		},

		//得到当前分组的index
		getGroupIndex: function(obj, id) {
			var index = 0;
			for(var i = 0; i < obj.length; i++) {
				if(id == obj[i].group_id) {
					index = i;
					break;
				}
			}
			return index;
		},

		//常规操作
		goodsActive: function(thisPanel, thisEdit, key) {
			var _this = this;

			//如果新拖入模块
			if(typeof(g_data[key].prop.size) == "undefined") {
				g_data[key].prop = {
					"size": "small",
					"titleshow": 0,
					"showprice": 1,
					"group_id_1": "",
					"group_id_2": ""
				};

			}

			/*icheck初始化*/
			$(thisEdit).find("input").iCheck(main.iCheckOption);

			//选择商品
			$(thisEdit).find(".choicegoods").unbind("click").click(function() {
				main.productDialog('isMuit=1', function(res) {

					if(res.length > 0) {
						var listmbE = $(thisEdit).find(".moban").html(),
							listmbP = $(thisPanel).find(".moban").html(); 
						for(var g in res) {
							var row = res[g];
							var newobj = {
								"Fhimg": row.Fhimg,
								"Fid": row.Fid,
								"Fsummary": row.Fsummary,
								"Flowprice": row.Flowprice,
								"Fbrand": row.Fbrand,
								"Fpersonnum": row.Fpersonnum
							};
							//加入编辑模板
							if($(thisEdit).find(".good-list-show ul li[rel='yuan']").length > 0) {
								$(thisEdit).find(".good-list-show ul li[rel='yuan']").eq(0).find(".com-list-box-choice-pic").html("<img src='" + newobj.Fhimg + "'>");
								$(thisEdit).find(".good-list-show ul li[rel='yuan']").eq(0).find(".c333").html("【" + newobj.Fbrand + "】" + newobj.Fsummary);
								$(thisEdit).find(".good-list-show ul li[rel='yuan']").eq(0).find(".goodsPricePrevious .mt10").html(newobj.Flowprice);
								$(thisEdit).find(".good-list-show ul li[rel='yuan']").eq(0).find(".goodsPriceNow .mt10").html(newobj.Flowprice);
								$(thisEdit).find(".good-list-show ul li[rel='yuan']").eq(0).find(".goodsSales .mt10").html(newobj.Fpersonnum);
								$(thisEdit).find(".good-list-show ul li[rel='yuan']").eq(0).attr("rel", "");

							} else {
								var newStrE = main.formatTemplate(newobj, listmbE);
								$(thisEdit).find(".good-list-show ul").append(newStrE);
							}

							//加入显示模板
							if($(thisPanel).find(".goods-list ul li[rel='yuan']").length > 0) {
								$(thisPanel).find(".goods-list ul li[rel='yuan']").eq(0).find("a").attr("href", newobj.mobile_url);
								$(thisPanel).find(".goods-list ul li[rel='yuan']").eq(0).find("img").attr("src", newobj.Fhimg);
								$(thisPanel).find(".goods-list ul li[rel='yuan']").eq(0).find(".goods-title").html(newobj.Fsummary + "[" + newobj.brand + newobj.type + "]");
								$(thisPanel).find(".goods-list ul li[rel='yuan']").eq(0).find(".goods-price").html("<i>￥</i>" + newobj.Flowprice);
								$(thisPanel).find(".goods-list ul li[rel='yuan']").eq(0).find(".goods-price").attr("yuandata", newobj.Flowprice);
								$(thisPanel).find(".goods-list ul li[rel='yuan']").eq(0).find("del").html("￥" + newobj.Flowprice);
								$(thisPanel).find(".goods-list ul li[rel='yuan']").eq(0).find(".goods-sale").html('已售' + newobj.Fpersonnum);
								$(thisPanel).find(".goods-list ul li[rel='yuan']").eq(0).find(".goods-stock").html('库存' + newobj.stock);
								$(thisPanel).find(".goods-list ul li[rel='yuan']").eq(0).attr("rel", "");

								$(thisPanel).find(".goods-list ul li").eq(g).find('.goods-stock').show();

							} else {
								var goodListLiLen = $(thisPanel).find(".goods-list ul li").length;
								var newStrP = main.formatTemplate(newobj, listmbP);
								$(thisPanel).find(".goods-list ul").append(newStrP);

							}
							//图片居中
							g_data[key].ItemList.push({
								"Ftype": 3,
								"Ftypeid": res[g].Fid
							});
							_this.goodsImgCenter(thisPanel, thisEdit, key);

						}
						$(thisEdit).find(".good-list-show ul li[rel='yuan']").remove();
						$(thisPanel).find(".goods-list ul li[rel='yuan']").remove();
					}

				});
			});

			//参数调整函数
			function propChange(thisPanel, thisEdit, key) {

				//显示风格
				$(thisEdit).find("input[name='size']").on('ifChecked', function() {
					var sizeVal = $(this).val();
					$(thisPanel).find(".goods-list").attr("class", "goods-list style-" + sizeVal);
					g_data[key].prop["size"] = sizeVal;
					_this.goodsImgCenter(thisPanel, thisEdit, key);
				});

				//显示价格
				$(thisEdit).find("input[name='showprice']:eq(0)").on('ifUnchecked', function() {
					$(thisPanel).find(".goods-price").html("");
					g_data[key].prop["showprice"] = 0;
				});
				$(thisEdit).find("input[name='showprice']:eq(0)").on('ifChecked', function() {
					$(thisPanel).find(".goods-price").each(function() {
						$(this).html($(this).attr("yuandata"));
					});
					g_data[key].prop["showprice"] = 1;
				});

			}

			//商品删除,上下移功能
			function imgDelete(thisPanel, thisEdit, key) {
				$(thisEdit).undelegate(".com-list-box-delete i", 'click').delegate(".com-list-box-delete i", 'click', function() {
					var shunxu = $(thisEdit).find(".com-list-box-delete i").index(this),
						style_num = g_data[key].prop.size;
					if($(thisPanel).find(".goods-list ul li").length <= 1) {
						$(thisPanel).find(".goods-list ul li:eq(0)").attr("rel", "yuan");
						$(thisPanel).find(".goods-list ul li:eq(0)").find("a").attr("href", "");
						$(thisPanel).find(".goods-list ul li:eq(0)").find("img").attr("src", app.baseUrl + "/seller/images/shop_decorate/pro_default_img.png");
						$(thisPanel).find(".goods-list ul li:eq(0)").find(".goods-title").html("第1个商品");
						$(thisPanel).find(".goods-list ul li:eq(0)").find(".goods-price").attr("yuandata", "价格");
						$(thisPanel).find(".goods-list ul li:eq(0)").find(".is_preferential").hide();
						$(thisPanel).find(".goods-list ul li:eq(0)").find(".is_vip").hide();
						$(thisPanel).find(".goods-list ul li:eq(0)").find(".goods-stock").hide();
						$(thisPanel).find(".goods-list ul li:eq(0)").find(".goods-price").html("<i>￥</i>价格");

						$(thisPanel).find(".goods-list ul li:eq(0)").find("del").hide();
						$(thisPanel).find(".goods-list ul li:eq(0)").find(".goods-sale").html("已售:数量");

						//图片居中
						_this.goodsImgCenter(thisPanel, thisEdit, key);
					} else {
						$(thisPanel).find(".goods-list ul li:eq('" + shunxu + "')").remove();
					}

					if($(thisEdit).find(".good-list-show ul li").length <= 1) {
						$(thisEdit).find(".good-list-show ul li:eq(0)").attr("rel", "yuan");
						$(thisEdit).find(".good-list-show ul li:eq(0)").find(".com-list-box-choice-pic").html("<div class='default-text'><span>商品</span><span>图片</span></div>");
						$(thisEdit).find(".good-list-show ul li:eq(0)").find(".c333").html("第1个商品");
						$(thisEdit).find(".good-list-show ul li:eq(0)").find(".goodsPricePrevious .mt10").html("0.0");
						$(thisEdit).find(".good-list-show ul li:eq(0)").find(".goodsPriceNow .mt10").html("0.0");
						$(thisEdit).find(".good-list-show ul li:eq(0)").find(".goodsSales .mt10").html("0.0");
					} else {
						$(this).parents("li").remove();
					}
					g_data[key].ItemList.splice(shunxu, 1);
				});

				//上移一个位置				
				$(thisEdit).undelegate(".com-list-box-operate .arrow-up", 'click').delegate(".com-list-box-operate .arrow-up", 'click', function() {
					if($(this).parents("li").find(".com-list-box-choice-pic img").length < 1) return;
					var index = $(thisEdit).find(".com-list-box-operate .arrow-up").index(this),
						htmlE = $(this).parents("li").prop("outerHTML"),
						htmlP = $(thisPanel).find(".goods-list ul li:eq(" + index + ")").prop("outerHTML");
					if(index > 0) {
						$(thisEdit).find(".good-list-show ul li:eq(" + (index - 1) + ")").before(htmlE);
						$(thisEdit).find(".good-list-show ul li:eq(" + (index + 1) + ")").remove();
						$(thisPanel).find(".goods-list ul li:eq(" + (index - 1) + ")").before(htmlP);
						$(thisPanel).find(".goods-list ul li:eq(" + (index + 1) + ")").remove();
						main.exchange(g_data[key].ItemList, (index - 1), index);
						console.log(g_data[key].ItemList);
					}
				});

				//下移一个位置
				$(thisEdit).undelegate(".com-list-box-operate .arrow-down", 'click').delegate(".com-list-box-operate .arrow-down", 'click', function() {
					if($(this).parents("li").find(".com-list-box-choice-pic img").length < 1) return;
					var index = $(thisEdit).find(".com-list-box-operate .arrow-down").index(this),
						lilen = $(thisEdit).find(".com-list-box-operate .arrow-down").length,
						htmlE = $(this).parents("li").prop("outerHTML"),
						htmlP = $(thisPanel).find(".goods-list ul li:eq(" + index + ")").prop("outerHTML");
					if(index < (lilen - 1)) {
						$(thisEdit).find(".good-list-show ul li:eq(" + (index + 1) + ")").after(htmlE);
						$(thisEdit).find(".good-list-show ul li:eq(" + index + ")").remove();
						$(thisPanel).find(".goods-list ul li:eq(" + (index + 1) + ")").after(htmlP);
						$(thisPanel).find(".goods-list ul li:eq(" + index + ")").remove();
						main.exchange(g_data[key].ItemList, index, (index + 1));
						console.log(g_data[key].ItemList);
					}
				});
			}

			propChange(thisPanel, thisEdit, key);
			imgDelete(thisPanel, thisEdit, key);

			//调用主模板JS
			main.saveClick();
			main.moduleActive();
			main.dragEdit();
		}

	}
});